//
//  NewsDetailViewController.m
//  News
//
//  Created by Han on 15-4-16.
//  Copyright (c) 2015年 IPingLun. All rights reserved.
//

#import "NewsDetailViewController.h"

@interface NewsDetailViewController () {
    NewsDetailPresenter *mPresenter;
    UITableView *mTableView;
    UIWebView *mWebView;
    NewsDetailTableHeaderView *mTableHeaderView;
    FLAnimatedImageView *mFLAnimatedImageView;
    
}

@end

@implementation NewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    mPresenter = NewsDetailPresenter.new;
    
    
    [self drawView];
    
    [self loadData];
}

- (void)loadData {
    
    [mPresenter getNewsDetail:self.newsId :^(NewsDetail *data) {

        [self loadWebView:data];

        
    } failureBlock:^(NSError *e) {

    }];

}

- (void)loadWebView:(NewsDetail *)data {
    
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    
    [mWebView loadHTMLString:data.content baseURL:baseURL];
    

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
    
    float webViewHeight = [result floatValue];
    
    CGRect frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, webViewHeight);
    mWebView.frame = frame;
    
    //NSLog(@"height=%f", webViewHeight);
    [mFLAnimatedImageView removeFromSuperview];
    [mWebView removeFromSuperview];
    
    mTableView = UITableView.new;
    mTableView.delegate = self;
    mTableView.dataSource = self;
    [self.view addSubview:mTableView];
     
    mTableHeaderView = [[NewsDetailTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, webViewHeight)];
    [mTableHeaderView addSubview:mWebView];
    mTableView.tableHeaderView = mTableHeaderView;
     
     
    [mTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(0);
        make.right.equalTo(self.view.right).offset(0);
        make.top.equalTo(self.view.top).offset(54);
        make.bottom.equalTo(self.view.bottom).offset(0);
    }];
    

}

/*
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
*/

- (void)drawView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *navigationBar = UIView.new;
    navigationBar.backgroundColor = NAV_COLOR;
    [self.view addSubview:navigationBar];
    
    UIView *parentView = self.view;
    
    [navigationBar makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(parentView.top).offset(0);
        make.left.equalTo(parentView.left).offset(0);
        make.right.equalTo(parentView.right).offset(0);
        make.height.equalTo(54);
    }];
    
    UIButton *backButton = UIButton.new;
    [backButton setImage:[UIImage imageNamed:@"back_nav_icon"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"back_nav_icon_press"] forState:UIControlStateHighlighted];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(5, 12.8, 7, 18.8);
    [backButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [navigationBar addSubview:backButton];
    
    [backButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navigationBar.top).offset(20);
        make.left.equalTo(navigationBar.left).offset(0);
        make.bottom.equalTo(navigationBar.bottom).offset(0);
        make.width.equalTo(44);
    }];
    
    
    mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 54, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 54)];
    //mWebView.hidden = YES;
    //mWebView.opaque = NO;
    mWebView.scrollView.scrollEnabled = NO;
    [mWebView setDelegate:(id<UIWebViewDelegate>)self];
    [self.view addSubview:mWebView];
    
    
    NSData *gifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"]];
    
    FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:gifData];
    mFLAnimatedImageView = [[FLAnimatedImageView alloc] init];
    mFLAnimatedImageView.animatedImage = image;
    [self.view addSubview:mFLAnimatedImageView];
    
    [mFLAnimatedImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view.centerY);
        make.centerX.equalTo(self.view.centerX);
        make.width.equalTo(180);
        make.height.equalTo(135);
    }];
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    
    CellIdentifier = @"NewsDetailTableCell";
    
    
    NewsDetailTableCell *cell = (NewsDetailTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[NewsDetailTableCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:CellIdentifier];
    }
    
    
    
    return cell;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

#pragma mark -----------------------------返回每个 cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}




-(void)clickBack {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
